package com.slb.slnwp.common.utils;

import com.google.common.collect.Maps;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.Map;

/**
 * 通用的响应类 ResponseJson，用于构建统一格式的接口响应
 * 包含成功，失败的返回定义
 * @author joinly
 * @date 2020-06-03 16:48
 * @desc: TODO
 */
@Data
public class ResponseJson<T> {

    @ApiModelProperty("返回状态")
    private Integer code;

    @ApiModelProperty("返回信息")
    private String msg;

    @ApiModelProperty("返回数据")
    private T data;

    public ResponseJson() {
    }

    public ResponseJson(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public ResponseJson(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static ResponseJson success() {
        ResponseJson responseJson =  new ResponseJson(ResponseCodeEnum.SUCCESS.getCode(), "操作成功");
        return responseJson;
    }

    public static ResponseJson success(Object data) {
        ResponseJson responseJson =  new ResponseJson(ResponseCodeEnum.SUCCESS.getCode(), "操作成功", data);
        return responseJson;
    }

    public static ResponseJson success(String msg, Object data) {
        ResponseJson responseJson =  new ResponseJson(ResponseCodeEnum.SUCCESS.getCode(), msg, data);
        return responseJson;
    }

    public static ResponseJson fail() {
        ResponseJson responseJson =  new ResponseJson(ResponseCodeEnum.FAIL.getCode(), "操作失败");
        return responseJson;
    }

    public static ResponseJson fail(String msg) {
        ResponseJson responseJson =  new ResponseJson(ResponseCodeEnum.FAIL.getCode(), msg);
        return responseJson;
    }

    public static ResponseJson fail(int code, String msg) {
        ResponseJson responseJson =  new ResponseJson(ResponseCodeEnum.FAIL.getCode(), msg);
        return responseJson;
    }

    public static ResponseJson success(int total, Object list) {
        Map<String, Object> data = Maps.newHashMap();
        data.put("total", total);
        data.put("list", list);
        ResponseJson responseJson =  new ResponseJson(ResponseCodeEnum.SUCCESS.getCode(), "操作成功", data);
        return responseJson;
    }

    public static ResponseJson fail(int code, String msg, Object data) {
        ResponseJson responseJson =  new ResponseJson(code, msg, data);
        return responseJson;
    }
}
